package com.wps.wdztv6.api.edit;

import com.wps.wdztv6.api.response.EditActiveSaveResponse;
import com.wps.wdztv6.api.response.EditCooperatorsResponse;
import com.wps.wdztv6.api.response.EditForceRefreshResponse;
import com.wps.wdztv6.api.response.EditLinkResponse;
import com.wps.wdztv6.common.HttpPostUtil;
import com.wps.wdztv6.utils.WPS4Util;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;

import java.util.Map;

@Log4j2
public class WdztEditApi {
    public static final String CONTENT_TYPE = "application/json";

    public static final String[] FILE_TYPES = {"w","s","p","f"};
    /**
     * 获取在线编辑链接
     * @param fileId 文件id
     * @param type 文件类型 w p s f
     * @return 在线编辑链接
     * @throws Exception
     */
    public static EditLinkResponse getEditUrl(String host, String fileId, String type, String wThirdParams) throws Exception {
        boolean isSupport = false;
        for (String fileType : FILE_TYPES) {
            if (StringUtils.equals(fileType, type)) {
                isSupport = true;
                break;
            }
        }
        if (!isSupport) {
            log.error("获取在线编辑，文档类型不支持:"+type);
            throw new RuntimeException("获取在线编辑，文档类型不支持:"+type);
        }
        String url = String.format("/api/edit/v1/files/%s/link?type=%s&%s",fileId,type,wThirdParams);
        Map<String,String> headers = WPS4Util.getSignatureHeaders(url, HttpMethod.GET.toString(),null,CONTENT_TYPE);
        return HttpPostUtil.sendGetRequest(host+url,headers, EditLinkResponse.class);
    }

    /**
     * 获取链接在线人数
     * @param host 中台地址
     * @param fileId 文件id
     */
    public static EditCooperatorsResponse getLinkCooperator(String host,String fileId) throws Exception {
        String url = "/api/edit/v2/files/{file_id}/cooperators";
        url = url.replace("{file_id}",fileId);
        Map<String,String> headers = WPS4Util.getSignatureHeaders(url, HttpMethod.POST.toString(),null,CONTENT_TYPE);
        log.info("请求地址: {}", host+url);
        return HttpPostUtil.sendGetRequest(host+url,headers, EditCooperatorsResponse.class);
    }

    /**
     * 强制刷新
     * @param host 中台地址
     * @param fileId 文件id
     */
    public static EditForceRefreshResponse forceRefreshLink(String host, String fileId) throws Exception {
        String url = "/api/edit/v2/files/{file_id}/refresh";
        url = url.replace("{file_id}",fileId);
        Map<String,String> headers = WPS4Util.getSignatureHeaders(url, HttpMethod.POST.toString(),null,CONTENT_TYPE);
        return HttpPostUtil.sendPostRequest(host+url,null,headers, EditForceRefreshResponse.class);
    }

    /**
     * 主动保存接口
     * @param host 中台地址
     * @param fileId 文件id
     */
    public static EditActiveSaveResponse activeSave(String host, String fileId,String timeout,Boolean forceSave) throws Exception {
        String url = "/api/edit/v1/file/save?file_id=%s&timeout=%s&force_save=%s";
        url = String.format(url,fileId,timeout,forceSave);
        Map<String,String> headers = WPS4Util.getSignatureHeaders(url, HttpMethod.POST.toString(),null,CONTENT_TYPE);
        return HttpPostUtil.sendPostRequest(host+url,null,headers, EditActiveSaveResponse.class);
    }
}
